#include <mxml.h>


#define NODE_NAME_CHINA 		"China"
#define NODE_NAME_CITY			"City"
#define NODE_NAME_NAME			"Name"
#define NODE_NAME_AREA			"Area"
#define NODE_NAME_POPULATION	"Population"
#define ATTRIBUTE_NAME_ISBIG	"isbig"


void show_xml(mxml_node_t * xml)
{
	/* 保存到字符串输出 */
	char buff[4096];
	mxmlSaveString(xml, buff, 4096, MXML_NO_CALLBACK);
	printf("%s\n", buff);

}


int main(){

	FILE *fp = fopen("China.xml","r");

	// 加载 xml 文件
	mxml_node_t * xml = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);

	// 找到第一个 city 节点
	mxml_node_t * city = mxmlFindElement(xml, xml, NODE_NAME_CITY, NULL, NULL, MXML_DESCEND);

	// 循环查找
	int whitespace = 0;
	while(city)
	{
		
		mxml_node_t * tmp = mxmlWalkNext(city, city, MXML_DESCEND_FIRST);
		printf("%s[%s] ", mxmlGetText(tmp, &whitespace),mxmlElementGetAttr(tmp, ATTRIBUTE_NAME_ISBIG));		// name

		tmp = mxmlWalkNext(tmp, city, MXML_NO_DESCEND);		// area
		printf("%s ", mxmlGetText(tmp, &whitespace));

		tmp = mxmlWalkNext(tmp, city, MXML_NO_DESCEND);		// populartion
		printf("%s\n", mxmlGetText(tmp, &whitespace));
		
		city = mxmlFindElement(city, xml, NODE_NAME_CITY, NULL, NULL, MXML_NO_DESCEND);		// 下一个 city
		
	}
	

	return 0;
}

